package com.example.actionsignin.mvp.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.actionsignin.R;
import com.example.actionsignin.entity.LoginEntity;
import com.example.actionsignin.http.HttpUtil;
import com.example.actionsignin.utils.ToastUtil;
import com.tencent.tauth.Tencent;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import io.reactivex.schedulers.Schedulers;

/**
 * 登录界面
 */
public class EntryActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "EntryActivity";
    private EditText mAccount;
    private EditText mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry);

        init();
    }

    private void init() {
        findViewById(R.id.entry_btn).setOnClickListener(this);
        findViewById(R.id.entry_enroll).setOnClickListener(this);

        mAccount = (EditText) findViewById(R.id.entry_account);
        mPassword = (EditText) findViewById(R.id.entry_password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.entry_btn:
                String telephone = mAccount.getText().toString();
                String password = mPassword.getText().toString();
                if (!telephone.equals("") && !password.equals(""))
                {
//                    writeAccount(telephone,password);
                    doLogin(telephone,password);
                }

                break;
            case R.id.entry_enroll:
                Intent intent1 = new Intent(EntryActivity.this,EnrollActivity.class);
                startActivity(intent1);
                break;
        }
    }

    private void doLogin(String telephone,String password)
    {
        HttpUtil.onSend().onLogin(telephone,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull LoginEntity loginEntity) {
                        if (loginEntity.getResult().equals("1"))
                        {
                            writeAccount(mAccount.getText().toString(),mPassword.getText().toString());
                            toMain();
                        }else {
                            ToastUtil.INSTANCE(getApplicationContext(),"请注册!");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        Disposables.disposed();
                    }
                });
    }

    private void writeAccount(String account,String password)
    {
        SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences("data",MODE_PRIVATE).edit();

        editor.putString("account",account);
        editor.putString("password",password);
        editor.apply();
    }

    private void toMain()
    {
        Intent intent = new Intent(EntryActivity.this,MainActivity.class);
        startActivity(intent);
        finish();
    }
}
